//
//  MXHomeDetialCommentsView.m
//  CatchBear
//
//  Created by 王启颖 on 2018/12/12.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomeDetialCommentsView.h"
#import "MXHomeDetialCommentsCell.h"
#import "MXCommentsModel.h"
#import "MXAuthController.h"

@interface MXHomeDetialCommentsView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic ,strong) UITableView *tableView;
@property (strong, nonatomic) UITextField *textField;

@property (nonatomic, assign) int currentPage;

@property (strong, nonatomic) NSMutableDictionary *dicParams;
@property (strong, nonatomic) NSMutableDictionary *sendDicParams;

@property (strong, nonatomic) NSMutableArray *paramsArr;
@end
@implementation MXHomeDetialCommentsView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.height-44)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_GRAY_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableView.estimatedRowHeight = 100;
        self.tableView.rowHeight=UITableViewAutomaticDimension;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        [self addSubview:self.tableView];
      
        
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 5, kScreenWidth - 20 - 84, 34)];
        self.textField.delegate = self;
        self.textField.placeholder = @"发表评论";
        self.textField.backgroundColor = MX_GRAY_COLOR;
        [self.textField reloadInputViews];
        UIView *textFView = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableView.height, kScreenWidth, 44)];
        textFView.backgroundColor = [UIColor whiteColor];
        UIView *textbgFV = [[UIView alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth - 5 - 84, 34)];
        textbgFV.backgroundColor = MX_GRAY_COLOR;
        [textFView addSubview:textbgFV];
        [textFView addSubview:self.textField];
        [self addSubview:textFView];
        
        UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sendButton.frame = CGRectMake(kScreenWidth - 88, 0, 88, 44);
        [sendButton setTitle:@"发送" forState:UIControlStateNormal];
        [sendButton setTitleColor:MX_TEXT1_COLOR forState:UIControlStateNormal];
        [sendButton addTarget:self action:@selector(sendClick:) forControlEvents:UIControlEventTouchUpInside];
        [textFView addSubview:sendButton];
        
    }
    return self;
}

-(void)sendClick:(UIButton *)button{
    [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
    NSString *token = [MXAccount sharedMXAccount].token;
    if (![token length]) {
        MXAuthController *authVC = [[MXAuthController alloc]init];
        [self.navigationController pushViewController:authVC animated:true];
        return;
    }
    
    [self sendMessageCommentsRequest];
    [self.textField resignFirstResponder];
}

-(void)setCommentParamsArr:(NSArray *)commentParamsArr{
    _commentParamsArr = commentParamsArr;
    
    self.paramsArr = [NSMutableArray arrayWithArray:_commentParamsArr];
    
    [self.tableView reloadData];
}

#pragma mark - UITextFieldDelegate -
/// 设置自定义键盘后，delegate 不会被调用？
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%@", [textField.text stringByReplacingCharactersInRange:range withString:string]);
    
    return YES;
}

#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.paramsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXHomeDetialCommentsCell *cell = [MXHomeDetialCommentsCell cellWithTableView:tableView];
    MXCommentsModel *model = [MXCommentsModel mj_objectWithKeyValues:_commentParamsArr[indexPath.row]];
    cell.model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.commentsTableBlock();
}

#pragma mark - 懒加载 -

-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

- (NSMutableDictionary *)sendDicParams {
    if (!_sendDicParams) {
        _sendDicParams = [NSMutableDictionary dictionary];
    }
    
    return _sendDicParams;
}

#pragma mark - 网络请求 -

-(void)sendMessageCommentsRequest{
    [SVProgressHUD show];
    
    self.sendDicParams[@"messages_id"] = self.messages_id;
    self.sendDicParams[@"comments_info"] = self.textField.text;
    self.sendDicParams[@"user_id"] = [MXUserMessageTool message].user_id;

    [[MXHttpManagerClass HttpRequstManager] PostURL:sendMessageComments params:self.sendDicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];

        if (code == 0) {
            NSLog(@"%@",result);
            self.commentsBlock();
            self.textField.text = @"";

        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }

    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}


@end
